var tp_arr = new Array(
    "/static/app/transverse/system/auth09/cw/index.htm",
    "/static/app/transverse/plugins/poup_confirm_del.htm"
);
var keywordStr = window.getUrlParam("k");
var startDate = window.getUrlParam("sd");
var endDate = window.getUrlParam("nd");
var bc = window.getUrlParam("bc");
var pageNo = window.getUrlParam("p");
var totle = window.getUrlParam("t");
var view = window.getUrlParam("v");
window.current = new Vue({
    el: '#current',
    template: window.getTemplate(tp_arr),
    data: {
        pages: [],
        totle: totle ? totle : 0,
        page: pageNo ? parseInt(pageNo) : 1,
        toPage: pageNo ? parseInt(pageNo) : 1,
        size: 10,
        keyword: keywordStr ? keywordStr : '',
        bc: bc ? bc : '0',
        startDate: startDate ? startDate : window.getDateTimeNow(),
        endDate: endDate ? endDate : window.getDateTimeNow(1),
        snList: [],
        snInfo: '',
        deLock: true,
        view: view ? view : '1',
        cwTotal: 0,
        cwUse: 0,
        supplierList: [],
        fSupplierCode: '0'
    },
    // 属性监听器
    watch: {
        'keyword': function (newVal, oldVal) {
            /*if (newVal != '') {
                if(window.current['deLock']) {
                    window.current['deLock'] = false;
                    setTimeout(function () {
                        window.get_sn_list();
                    },1000);
                }
            }*/
        }
    },
    filters: {
        _formatDate: function (val) {
            return window.dateTimeFormat(val, "yyyy-MM-dd h:m:s");
        },
        _getRowsColor: function (index) {
            var className = 'even';
            if (index % 2 == 0) {
                className = '';
            }
            return className;
        },
        _formatNumber: function (val) {
            val = parseInt(val);
            if (isNaN(val)) {
                val = 0;
            }
            return val;
        },
        _currPage: function (val) {
            var page = window.current['page'];
            var pages = window.current['pages'].length;
            val = parseInt(val);
            var className = [];
            if (pages > 9 && page <= 5 && val > 9) {
                className.push('display-none');
            } else if (pages > 9 && (pages - page < 5)) {
                if (pages - val >= 9) {
                    className.push('display-none');
                }
            } else if (pages > 9 && page > 5 && (val < page - 4 || val > page + 4)) {
                className.push('display-none');
            }
            /*if(pages > 9 && page > 5 && ((page - 4 > val) || (page + 4 < val))){
                className.push('display-none');
            }*/
            if (page == val) {
                className.push('active');
            }
            return className;
        },
        _currPageLeft: function () {
            var page = window.current['page'];
            var pagesArr = window.current['pages'];
            var pages = pagesArr ? pagesArr.length : 0;
            var className = [];
            if (!(pages > 9 && page > 5)) {
                className.push('display-none');
            }
            return className;
        },
        _currPageRight: function () {
            var page = window.current['page'];
            var pagesArr = window.current['pages'];
            var pages = pagesArr ? pagesArr.length : 0;
            var className = [];
            if (pages <= 9 || (pages - page < 5)) {
                className.push('display-none');
            }
            return className;
        },
        _fIfFinishType: function (val) {
            var name;
            switch (val) {
                case "0":
                    name = '未完成';
                    break;
                case "1":
                    name = '已完成';
                    break;
                default:
                    name = '未知';
                    break;
            }
            return name;
        },
        _fCFlagType: function (val) {
            var name;
            switch (val) {
                case "0":
                    name = '未审核';
                    break;
                case "1":
                    name = '已审核';
                    break;
                case "2":
                    name = '反审核';
                    break;
                default:
                    name = '未知';
                    break;
            }
            return name;
        },
        _fIfCloseType: function (val) {
            var name;
            switch (val) {
                case "0":
                    name = '未结案';
                    break;
                case "1":
                    name = '自动结案';
                    break;
                case "2":
                    name = '反结案';
                    break;
                case "3":
                    name = '手工结案';
                    break;
                default:
                    name = '未知';
                    break;
            }
            return name;
        }
    },
    computed: {},
    methods: {
        onSearch: function () {
            window.get_sn_list_to_page_one();
        },
        onClear: function () {
            this['keyword'] = '';
            window.get_sn_list_to_page_one();
        },
        onDel: function (index) {
            window.del_sn_to_confirm(index);
        },
        onDelSn: function () {
            window.del_sn_info();
        },
        onSetPage: function (val) {
            var page = this['page'];
            var totle = this['totle'];
            var keyword = this['keyword'];
            var startDate = this['startDate'];
            var endDate = this['endDate'];
            if (page != val) {
                var hrefStr = window.location.pathname + '?a=1';
                if ((keywordStr == null && keyword == '') || keywordStr == keyword) {
                    hrefStr += '&p=' + val + '&t=' + totle;
                }
                if (keyword != '') {
                    hrefStr += '&k=' + keyword;
                }
                if (startDate != '') {
                    hrefStr += '&sd=' + startDate;
                }
                if (endDate != '') {
                    hrefStr += '&nd=' + endDate;
                }
                if (cw != '') {
                    hrefStr += '&cw=' + cw;
                }
                if (ct != '') {
                    hrefStr += '&ct=' + ct;
                }
                if (wg != '') {
                    hrefStr += '&wg=' + wg;
                }
                window.location.href = hrefStr;
            }
        },
        onToPage: function () {
            var page = this['page'];
            var to_page = this['toPage'];
            var totle = this['totle'];
            var keyword = this['keyword'];
            var startDate = this['startDate'];
            var endDate = this['endDate'];
            var pagesArr = this['pages'];
            var cw = this['cw'];
            var ct = this['ct'];
            var wg = this['wg'];
            var pages = pagesArr ? pagesArr.length : 0;
            if (to_page > pages) {
                window.custom_toast('无法跳转该页面！');
                return;
            }
            if (page != to_page) {
                var hrefStr = window.location.pathname + '?a=1';
                if ((keywordStr == null && keyword == '') || keywordStr == keyword) {
                    hrefStr += '&p=' + to_page + '&t=' + totle;
                }
                if (keyword != '') {
                    hrefStr += '&k=' + keyword;
                }
                if (startDate != '') {
                    hrefStr += '&sd=' + startDate;
                }
                if (endDate != '') {
                    hrefStr += '&nd=' + endDate;
                }
                if (cw != '') {
                    hrefStr += '&cw=' + cw;
                }
                if (ct != '') {
                    hrefStr += '&ct=' + ct;
                }
                if (wg != '') {
                    hrefStr += '&wg=' + wg;
                }
                window.location.href = hrefStr;
            }
        },
        onMoreSnList: function () {
            var page = this['page'];
            var size = this['size'];
            var totle = this['totle'];
            if (page * size < totle) {
                window.get_sn_list_more();
            }
        },
        onRefresh: function () {
            //_currPageRightwindow.location.reload();
            window.get_mo_list();
        },
        onSelectView: function (type) {
            var view = this['view'];
            if (view != type) {
                this['view'] = type;
                window.get_sn_list_to_page_one();
            }
        },
        onRadioChecked: function (key, val) {
            if (this[key] != val) {
                this[key] = val;
                window.get_sn_list_to_page_one();
            }
        },
        importExcel : function () {
            window.loading();
            var formData = new window.FormData();
            formData.append('excel', document.querySelector('input[type=file]').files[0]);
            $.ajax({
                type: 'post',
                url: '/system/Auth09/importExcel',
                cache: false,
                contentType: false,
                processData: false, //默认为true，默认情况下，发送的数据将被转换为对象，设为false不希望进行转换
                data: formData, //数据
                //dataType: "json",
                success: function(data, textStatus, jqXHR) {
                    window.unloading();
                    console.log("请求返回success信息data：", data);
                    window.custom_toast('导入成功');window.current.onSearch();return;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    window.unloading();
                    window.custom_toast('导入失败');return;
                }
            });
            $("input[type=file]").eq(0).val("");
        },
        onExportExcel: function () {
            window.export_excel();
        }
    }
});

window.get_sn_list_to_page_one = function () {
    window.current['page'] = 1;
    window.current['snList'] = [];
    window.current['totle'] = 0;
    window.get_sn_list();
}

window.get_sn_list_more = function () {
    var page = window.current['page'];
    window.current['page'] = parseInt(page) + 1;
    window.get_sn_list();
}

window.get_sn_list = function () {
    var keyword = window.current['keyword'];
    var startDate = window.current['startDate'];
    var endDate = window.current['endDate'];
    var bc = window.current['bc'];
    var fSupplierCode = window.current['fSupplierCode'];
    var page = window.current['page'];
    var size = window.current['size'];
    var view = window.current['view'];
    if (keyword != '') {
        window.current['page'] = 1;
        window.current['totle'] = 0;
        page = 1;
    }
    var data_arr = {
        keyword: keyword,
        fStartDate: startDate,
        fEndDate: endDate,
        bc: bc,
        fSupplierCode: fSupplierCode,
        page: page,
        size: size,
        view: view
    };
    window.ajax({
        url: "/service/Goods/getSnListCw",
        data: data_arr,
        async: false,
        success: function (d) {
            if (keyword != '') {
                window.current['keyword'] = '';
                window.current['snList'] = [];
            }
            if (d['status'] == 1) {
                var totle = window.current['totle'];
                var snList = window.current['snList'];
                var list = d['data'];
                if (list) {
                    if (view == '1') {
                        var cwTotal = 0;
                        var cwUse = 0;
                        $.each(list, function (i, info) {
                            cwTotal += parseInt(info['fTotal']);
                            cwUse += parseInt(info['fUse']);
                            snList.push(info);
                        });
                        window.current['cwTotal'] = cwTotal;
                        window.current['cwUse'] = cwUse;
                        window.current['snList'] = snList;
                    }
                    if (view == '2') {
                        $.each(list, function (i, info) {
                            snList.push(info);
                        });
                        window.current['snList'] = snList;
                        if (totle == 0) {
                            window.get_sn_list_count();
                        }
                    }
                }
            } else {
                var msg = '';
                switch (d['code']) {
                    case '3307':
                        msg = keyword + '这个编码无法识别型号';
                        break;
                    default:
                        msg = d['msg'];
                        break;
                }
                window.custom_toast(msg);
            }
            //window.current['deLock'] = true;
        }
    });
}

window.get_sn_list_count = function () {
    var keyword = window.current['keyword'];
    var startDate = window.current['startDate'];
    var endDate = window.current['endDate'];
    var bc = window.current['bc'];
    var fSupplierCode = window.current['fSupplierCode'];
    var data_arr = {
        keyword: keyword,
        fStartDate: startDate,
        fEndDate: endDate,
        bc: bc,
        fSupplierCode: fSupplierCode
    };
    window.ajax({
        url: "/service/Goods/getSnListCwCount",
        data: data_arr,
        async: false,
        success: function (d) {
            if (d['status'] == 1) {
                var totle = parseInt(d['data']);
                window.current['totle'] = totle;
            } else {
                window.custom_toast(d['msg']);
            }
        }
    });
}

window.get_supplier_list = function () {
    var data_arr = {};
    window.ajax({
        url: "/service/Goods/getSupplierListCw",
        data: data_arr,
        async: false,
        success: function (d) {
            if (d['status'] == 1) {
                var list = d['data'];
                window.current['supplierList'] = list;
            } else {
                window.custom_toast(d['msg']);
            }
        }
    });
}

window.del_sn_to_confirm = function (index) {
    window.current['snInfo'] = window.current['snList'][index];
    window.poup('#confirm_del');
}

window.del_sn_info = function () {
    var snInfo = window.current['snInfo'];
    var data_arr = {
        barcode_cw_uuid: snInfo['barcode_cw_uuid']
    };
    window.ajax({
        url: "/service/Goods/delSnInfoCw",
        data: data_arr,
        async: false,
        success: function (d) {
            if (d['status'] == 1) {
                window.poup_close('#confirm_del');
                window.custom_toast('删除成功！');
                window.get_sn_list_to_page_one();
            } else {
                window.custom_toast(d['msg']);
            }
        }
    });
}

window.setPages = function (count) {
    var size = window.current['size'];
    var pageCount = Math.ceil(count / size);
    var pages = [];
    for (let i = 1; i <= pageCount; i++) {
        pages.push(i);
    }
    window.current['pages'] = pages;
}

window.export_excel = function () {

    var keyword = window.current['keyword'];
    var startDate = window.current['startDate'];
    var endDate = window.current['endDate'];
    var bc = window.current['bc'];
    var fSupplierCode = window.current['fSupplierCode'];
    var view = window.current['view'];

    var data_arr = {
        keyword: keyword,
        fStartDate: startDate,
        fEndDate: endDate,
        bc: bc,
        fSupplierCode: fSupplierCode,
        view: view
    };

    var form = $('#exportExcelForm');
    form.html('');
    $.each(data_arr, function (key, val) {
        var inputHTML = $("<input type='hidden' name='" + key + "'/>");
        inputHTML.attr("value", val);
        form.append(inputHTML);
    });
    form.submit();
}

window.get_sn_list();
window.get_supplier_list();

//日期范围限制
var start = {
    elem: '#start',
    format: 'YYYY-MM-DD hh:mm:ss',
    istime: true, //是否开启时间选择
    isclear: false, //是否显示清空
    istoday: false, //是否显示今天
    choose: function (datas) {
        window.current['startDate'] = datas;
        window.get_sn_list_to_page_one();
    }
};

var end = {
    elem: '#end',
    format: 'YYYY-MM-DD hh:mm:ss',
    istime: true, //是否开启时间选择
    isclear: false, //是否显示清空
    istoday: false, //是否显示今天
    choose: function (datas) {
        window.current['endDate'] = datas;
        window.get_sn_list_to_page_one();
    }
};
laydate(start);
laydate(end);
